package com.janal.controller.Postbar.bar;

import com.janal.model.Postbar.bar.Post_bar;
import com.janal.service.Postbar.bar.BarServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Lenovo
 */
public class CreatePostbarServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、从前端获取创建贴吧的信息
        HttpSession session = req.getSession();
        String barHost = (String) session.getAttribute("usercode");
        String barId = req.getParameter("bar_id");
        String barName = req.getParameter("bar_name");
        String title = req.getParameter("title");
        //2、判断信息不能为空
        if("".equals(barId) || "".equals(barName)){
            req.setAttribute("createBarError","贴吧id 或 贴吧名 不能为空！");
            req.getRequestDispatcher("createPostbar.jsp").forward(req,resp);
            return;
        }
        Post_bar pb = new Post_bar(barId,barName,barHost,title);
        //3、调用service存储贴吧信息
        BarServiceImpl cps = new BarServiceImpl();
        if (!cps.createBarService(pb)) {
            req.setAttribute("createBarError","贴吧id 或 贴吧名 已存在！");
        } else {
            req.setAttribute("createBarSuccess","已发送创建申请，待审核！");
        }
        req.getRequestDispatcher("createPostbar.jsp").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
